package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.StrategyEs;
import cn.wolfcode.wolf2w.business.repository.StrategyEsRepository;
import cn.wolfcode.wolf2w.business.service.IMessageSrvice;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.strategy.entity.Strategy;
import com.alibaba.fastjson2.JSON;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceImpl implements IMessageSrvice {

    @Autowired
    private StrategyEsRepository strategyEsRepository;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //TODO: 接收消息
    @RabbitListener(queues = {"esQueue"})
    public void onEsMessage(String message){
        Strategy strategy = JSON.parseObject(message, Strategy.class);
        StrategyEs es = new  StrategyEs();
        BeanUtils.copyProperties(strategy,es);
        strategyEsRepository.save(es);

        //redis删除消息
        String key = RedisKeys.ES_DATA.join("key");
        redisTemplate.opsForZSet().remove(key,message);


    }

}
